package com.example.base.mvvm

import androidx.lifecycle.ViewModel
import com.example.base.net.ApiService
import com.example.base.net.RetrofitUtils
import com.google.gson.Gson
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

/**
 * mvvm之viewmodel
 */
open class BaseViewModel:ViewModel() {
    val apiService = RetrofitUtils.getRetrofit().create(ApiService::class.java)

    //封装map转成body的工具类
    fun mapToBody(map:Map<String,Any>):RequestBody{
        val json = Gson().toJson(map)
        val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(),json)
        return body
    }
}